import md5 from 'blueimp-md5'
const urlParams = getUrlParams()
export default function cryptoMd5(obj: any={}) {
  const key = `${urlParams.channel}#${urlParams.partner_id}`
  const newObj = Object.assign(obj,{
    channel: urlParams.channel,
    partner_id: urlParams.partner_id
  })
  var objKeys = Object.keys(newObj).sort();
  var values = '';
  objKeys.forEach(function (key) {
    if(key != 'files'){
      values += `${key}=${obj[key]}&`
    }
  });
  values+='key='+ key
  const sign = md5(values)
  const result = {
    ...newObj,
    sign
  }
  if(obj.hasOwnProperty('files')){
    result.files = obj.files
  }

  return result
}
function getUrlParams(){
  const urlParams = new URL(location.href).searchParams
    const params:any = {}
    for (const [key, value] of urlParams.entries()) {
      params[key] = value
    }
    return params
}